 /**
 * Description: 数据字典控制器
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-17 上午 11:21:47
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-17   xk       0.0.1     Initial
 */
package com.metro.demo.sys.controller;

import com.metro.core.annotation.BusinessDesc;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import com.metro.demo.sys.service.inf.ISysDictionaryService;
import com.metro.demo.sys.beans.vo.SysDictionary;
import com.metro.demo.web.controller.BaseController;
import java.util.List;

 /**
 * 数据字典控制器<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-17]
 */
@Controller
@RequestMapping("/sysDictionary")
public class SysDictionaryController extends BaseController {
    //菜单编码
    private static final String menuCode = "SYS_DICTIONARY";
    //模块描述
    private static final String MODULE_DESC = "数据字典";
    @Autowired
    private ISysDictionaryService sysDictionaryService;
    @Override
    protected String getViewDir() {
        return "sys/";
    }
    
    /**
     * 去查询数据字典
     * 
     * @param sysDictionary 数据字典页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-17]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询数据字典列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "tree", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView tree(SysDictionary sysDictionary) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("sysdictionary_tree"));
            mav.addObject("searchEntity", sysDictionary);
        } catch (Exception ex) {
            log.error("查询数据字典出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询数据字典
     *
     * @param sysDictionary 数据字典页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-17]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询数据字典列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "queryForTree", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult queryForTree(QueryParams<SysDictionary> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setOrderColumn("inner_code");
            List<SysDictionary> list = sysDictionaryService.queryForList(queryParams);
            jr.setData(list);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询数据字典出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 新增数据字典
     * 
     * @param entity 数据字典页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-17]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增数据字典")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveSysDictionary(SysDictionary entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysDictionaryService.save(entity);
            jr.setResult(res);
            log.debug("新增数据字典成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增数据字典出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除数据字典
     * 
     * @param id 数据字典页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-17]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除数据字典")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除数据字典id={}",id);
            IResult codeMsg = sysDictionaryService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除数据字典出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 修改数据字典
     * 
     * @param entity 数据字典页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-17]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改数据字典")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateSysDictionary(SysDictionary entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysDictionaryService.update(entity);
            jr.setResult(res);
            log.debug("修改数据字典成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改数据字典出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改部门")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "batchUpdate", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult batchUpdateSysDictionary(@RequestBody List<SysDictionary> sysDictionarys) {
        JsonResult jr = new JsonResult();
        try {
                IResult res = sysDictionaryService.batchUpdateSysDictionary(sysDictionarys);
                jr.setResult(res);
                log.debug("修改数据字典成功【{}】", sysDictionarys);
            } catch (Exception ex) {
                log.error("修改数据字典出错", ex);
                jr.setResult(ResultCode.ERROR);
            }
        return jr;
    }
    
    /**
     * 去查看数据字典
     * 
     * @param id 数据字典页面表单对象唯一标识
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-17]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看数据字典详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    @ResponseBody
    public JsonResult info(@PathVariable Long id) {
        JsonResult jr = new JsonResult();
        try {
            jr.setData(sysDictionaryService.getById(id));
        } catch (Exception ex) {
            log.error("查看数据字典出错", ex);
        }
        return jr;
    }
    
}
